package com.bsoft.gol.hcs.utils;

import com.google.common.cache.CacheBuilder;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author tongtp
 * @version v0.1
 * @Package utils
 * @className BaseCache
 * @description
 * @create 2020-09-08 14:40
 **/
public class BaseCache<K, V> implements Cache<K, V> {
	public final com.google.common.cache.Cache<K, V> cache;

	public BaseCache(long size, long duration) {
		this.cache = CacheBuilder.newBuilder().maximumSize(size).expireAfterWrite(duration, TimeUnit.SECONDS).build();
	}

	@Override
	public V get(K key) {
		return this.cache.getIfPresent(key);
	}

	@Override
	public void put(K key, V value) {
		this.cache.put(key, value);
	}

	@Override
	public void remove(K key) {
		Map<K, V> map = this.cache.asMap();
		map.remove(key);
	}
}
